#include"game.h"
#include"memory.h"
#include<QString>
#include<QImage>
game::game(){
    getmap();
}

void game::getmap(){
    int nummap1[8][8]={
        1,1,1,1,1,1,1,1,
        1,3,4,0,0,0,0,1,
        1,0,1,1,1,0,1,1,
        1,0,0,5,0,0,0,1,
        1,0,1,4,1,0,1,1,
        1,0,1,3,1,0,1,1,
        1,0,0,0,0,4,3,1,
        1,1,1,1,1,1,1,1,
    };
    int nummap2[8][8]={
        1,1,1,1,1,1,1,1,
        1,1,0,0,3,0,1,1,
        1,0,0,0,1,0,1,1,
        1,0,3,4,0,0,1,1,
        1,0,0,1,4,1,1,1,
        1,1,0,5,0,1,1,1,
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,8,1
    };
    memcpy(&map[0][0],&nummap2[0][0],sizeof(int)*64);
}
void game::drawmap(QPainter *pdraw){
    QString imgURL;
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){


        switch (map[i][j]){
        case 0://地
            imgURL="://image/ground.jpg";
            break;
        case 1://墙
            imgURL="://image/wall.jpg";
            break;

        case 3://目标
            imgURL="://image/target3.jpg";
            break;
        case 4://箱子
            imgURL="://image/box4.jpg";
            break;
        case 5://人
            imgURL="://image/man5.webp";
            break;
        case 7://

        case 8://目标完成
            imgURL="://image/targetdone8.webp";
            break;
        }

        QImage image(imgURL);
        pdraw->drawImage(QRect(j*image.width(),i*image.height(),image.width(),image.height()),image);
    }}
}
void game::Key(QKeyEvent *event){
    int i=getr();
    int j=getc();
    switch(event->key()){
    case Qt::Key_W:
        if(map[i-1][j]==0||map[i-1][j]==3){
            map[i][j]-=5;
            map[i-1][j]+=5;
        }
        if(map[i-1][j]==4||map[i-1][j]==7){
            if(map[i-2][j]==0||map[i-2][j]==3){
                map[i][j]-=5;
                map[i-1][j]+=1;
                map[i-2][j]+=4;
            }
        }
        break;
    case Qt::Key_S:
        if (map[i + 1][j] == 0 || map[i + 1][j] == 3)
        {
             map[i][j] -= 5;
             map[i + 1][j] += 5;
        }
                    if (map[i + 1][j] == 4 || map[i + 1][j] == 7)
                    {
                        if (map[i + 2][j] == 0 || map[i + 2][j] == 3)
                        {
                            map[i][j] -= 5;
                            map[i + 1][j] += 1;
                            map[i + 2][j] += 4;
                        }
                    }
                    break;
    case Qt::Key_A:
        if (map[i][j - 1] == 0 || map[i][j - 1] == 3)
                   {
                       map[i][j] -= 5;
                       map[i][j - 1] += 5;
                   }
                   if (map[i][j - 1] == 4 || map[i][j - 1] == 7)
                   {
                       if (map[i][j - 2] == 0 || map[i][j - 2] == 3)
                       {
                           map[i][j] -= 5;
                           map[i][j - 1] += 1;
                           map[i][j - 2] += 4;
                       }
                   }
                   break;
    case Qt::Key_D:
        if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
                   {
                       map[i][j] -= 5;
                       map[i][j + 1] += 5;
                   }
                   if (map[i][j + 1] == 4 || map[i][j + 1] == 7)
                   {
                       if (map[i][j + 2] == 0 || map[i][j + 2] == 3)
                       {
                           map[i][j] -= 5;
                           map[i][j + 1] += 1;
                           map[i][j + 2] += 4;
                       }
                   }
                   break;
    }
}
int game::getr(){
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            if(map[i][j]==5||map[i][j]==8){
                return i;
            }

        }
    }
    return -1;
}
int game::getc(){
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            if(map[i][j]==5||map[i][j]==8){
                return j;
            }

        }
    }
    return -1;
}
int game::gameover(){
    for(int i=0;i<8;i++)
       {
           for(int j=0;j<8;j++)
           {
               if(map[i][j]==4)
               {
                   return 0;
               }
           }
       }
       return 1;
}
